<?php 
class ownerModel extends CDBModelBase {
	public function getTabelName(){
		return "t_owner";
	}

	public function getPKID(){
		return "owner_id";
	}
	
	protected function getSequence()
	{
		return "OWNER_SEQUENCE";
	}
	
	protected $_moneyColumns = array("c_discount","balance");
	
	public function select($owner_id)
	{
		$param = array(array(":id",$owner_id,PDO::PARAM_STR));
		$sql = "select o.*,c.province,c.city,c.area from ".$this->getTabelName()." o,t_community c where o.community_id=c.community_id and o.del_flag=0 and ".$this->getPKID()."=:id";

		return $this->db->executeFetch($sql,$param);
	}

	public function checkCard($card,$filed,$id = 0)
    {
        $params = array(array(":card",$card,PDO::PARAM_STR));
        if("supplementary_card"==$filed)
        {
        	$where = "(supplementary_card=:card or user_name=:card)";
        }
        else{
        	$where = $filed."=:card";
        }
        
        if($id)
        {
        	$params[] = array(":id",$id,PDO::PARAM_INT);
        	$where .= " and owner_id<>:id";
        }
		$count = $this->db->executeScalar(
				"select count(*) from ".$this->getTabelName()." where ".$where,$params);
		
		return $count>0?true:false;
    }
    
    /**
	 * 验证用户名是否重复
	 * @param string $username
	 * @param int $id
	 * @return boolean
	 */
	public function checkUserName($username,$id = 0)
    {
        $params = array(array(":user_name",$username,PDO::PARAM_STR));
        $where = "(supplementary_card=:user_name or user_name=:user_name)";
	    if($id)
        {
        	$params[] = array(":id",$id,PDO::PARAM_INT);
        	$where .= " and owner_id<>:id";
        }
		$count = $this->db->executeScalar(
				"select count(*) from ".$this->getTabelName()." where ".$where,$params);

		return $count>0?true:false;
    }
    
	/**
     * 业主登录
     * @param string $userName 用户名
     * @param string $pwd 密码
     */
    public function userLogin($userName, $pwd)
    {
    	$param = array(
    			array(":username",$userName,PDO::PARAM_STR),
    			array(":password",$pwd,PDO::PARAM_STR)
    	);
    
    	return $this->db->executeFetch("select owner_id,user_name,owner_name,first_login from ".$this->getTabelName()." where del_flag=0 and (user_name=:username or supplementary_card=:username) and user_pwd=:password",$param);
    }
    
	/**
     * 验证交易密码是否正确(扣减积分时使用)
     * @param int $shopID
     * @param string $pwd
     */
    public function checkPayPwd($ownerID,$pwd)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":pay_pwd",$pwd,PDO::PARAM_STR)
    	);
    	
    	$count = $this->db->executeScalar("select count(0) from ".$this->getTabelName()." where del_flag=0 and card_status=1 and pay_status=1 and owner_id=:owner_id and pay_pwd=:pay_pwd",$param);
    	
    	return $count>0?true:false;
    }
    
    /**
     * 验证登录密码是否正确
     * @param $ownerID
     * @param $pwd
     */
    public function checkUserPwd($ownerID,$pwd)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":user_pwd",$pwd,PDO::PARAM_STR)
    	);
    	 
    	$count = $this->db->executeScalar("select count(0) from ".$this->getTabelName()." where del_flag=0 and owner_id=:owner_id and user_pwd=:user_pwd",$param);
    	 
    	return $count>0?true:false;
    }
    
    /**
     * 验证交易密码是否正确(业主后台使用)
     * @param $ownerID
     * @param $pwd
     */
	public function checkPayPwdByOwner($ownerID,$pwd)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":pay_pwd",$pwd,PDO::PARAM_STR)
    	);
    	
    	$count = $this->db->executeScalar("select count(0) from ".$this->getTabelName()." where del_flag=0 and owner_id=:owner_id and pay_pwd=:pay_pwd",$param);
    	
    	return $count>0?true:false;
    }
    
    /**
     * 修改交易状态
     * @param $ownerID
     * @param $type
     */
    public function changePayStatus($ownerID,$type)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":pay_status",$type,PDO::PARAM_STR)
    	);
    	return $this->db->executeNonQuery("update ".$this->getTabelName()." set pay_status=:pay_status where owner_id=:owner_id",$param);
    }
    
    /**
     * 修改卡片状态
     * @param $ownerID
     * @param $type
     */
 	public function changeCardStatus($ownerID,$type)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":card_status",$type,PDO::PARAM_STR)
    	);
    	return $this->db->executeNonQuery("update ".$this->getTabelName()." set card_status=:card_status where owner_id=:owner_id",$param);
    }
    
	/**
     * 修改交易状态
     * @param $ownerID
     * @param $type
     */
    public function changeCardStatusForShop($ownerID,$shopID,$type)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":shop_id",$shopID,PDO::PARAM_STR),
    			array(":pay_status",$type,PDO::PARAM_STR)
    	);
    	return $this->db->executeNonQuery("update t_owner_shop set owner_card_status=:pay_status where del_flag=0 and shop_id=:shop_id and owner_id=:owner_id",$param);
    }
    
    /**
     * 修改所有商户的交易状态
     * @param unknown_type $ownerID
     * @param unknown_type $type
     */
	public function changeCardStatusForAllShop($ownerID,$type)
    {
    	$param = array(
    			array(":owner_id",$ownerID,PDO::PARAM_STR),
    			array(":pay_status",$type,PDO::PARAM_STR)
    	);
    	return $this->db->executeNonQuery("update t_owner_shop set owner_card_status=:pay_status where del_flag=0 and owner_id=:owner_id",$param);
    }
    
    /**
     * 验证找回密码的用户名和邮箱是否正确
     * @param string $username
     * @param string $mail
     */
    public function checkForgetInfo($username,$mail)
    {
    	$param = array(
    			array(":username",$username,PDO::PARAM_STR),
    			array(":mail",$mail,PDO::PARAM_STR)
    	);
    	 
    	return $this->db->executeScalar("select owner_id from ".$this->getTabelName()." where del_flag=0 and user_name=:username and mail=:mail",$param);
    }
}
?>
